package com.walkline.dianping.dao;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import com.walkline.dianping.DianpingException;
import com.walkline.dianping.DianpingSDK;
import com.walkline.dianping.inf.Business;
import com.walkline.dianping.inf.SingleDeal;
import com.walkline.util.Function;
import com.walkline.util.StringUtility;

public class DianpingSingleDeal extends DianpingObject implements SingleDeal
{
	private String _deal_id = "";
	private String _title = "";
	private String _description = "";
	private String _city = "";
	private String _list_price = "";
	private String _current_price = "";
	private String[] _regions;
	private String[] _categories;
	private int _purchase_count = 0;
	private String _purchase_deadline = "";
	private String _publish_date = "";
	private String _details = "";
	private String _image_url = "";
	private String _small_image_url = "";
	private String[] _more_image_urls;
	private String[] _more_small_image_urls;
	private int _is_popular = 0;
	private int _is_reservation_required = 0;
	private int _is_refundable = 0;
	private String _special_tips = "";
	private String _notice = "";
	private String _deal_url = "";
	private String _deal_html5_url = "";
	private double _commission_ratio = 0;
	private Business[] _businesses;
	private int _businesses_count = 0;

	public DianpingSingleDeal(DianpingSDK dianping, JSONObject jsonObject) throws DianpingException
	{
		super(dianping, jsonObject);

		try {
			JSONObject deal = jsonObject;

			if (deal != null)
			{
				_deal_id = deal.optString("deal_id");
				_title = deal.optString("title");
				_description = deal.optString("description");
				_city = deal.optString("city");
				_list_price = StringUtility.formatDoubleValue(deal.getDouble("list_price"));
				_current_price = StringUtility.formatDoubleValue(deal.getDouble("current_price"));

				JSONArray regions = deal.optJSONArray("regions");
				_regions = new String[regions.length()];
				for (int i=0; i<regions.length(); i++) {_regions[i] = (String) regions.get(i);}

				JSONArray categories = deal.optJSONArray("categories");
				_categories = new String[categories.length()];
				for (int i=0; i<categories.length(); i++) {_categories[i] = (String) categories.get(i);}

				_purchase_count = deal.optInt("purchase_count");
				_purchase_deadline = deal.optString("purchase_deadline");
				_publish_date = deal.optString("publish_date");
				_details = deal.optString("details");
				_image_url = deal.optString("image_url");
				_small_image_url = deal.optString("s_image_url");

				JSONArray more_image_urls = deal.optJSONArray("more_image_urls");
				_more_image_urls = new String[more_image_urls.length()];
				for (int i=0; i<more_image_urls.length(); i++) {_more_image_urls[i] = (String) more_image_urls.get(i);}

				JSONArray more_small_image_urls = deal.optJSONArray("more_s_image_urls");
				_more_small_image_urls = new String[more_small_image_urls.length()];
				for (int i=0; i<more_small_image_urls.length(); i++) {_more_small_image_urls[i] = (String) more_small_image_urls.get(i);}

				_is_popular = deal.optInt("is_popular");
				_is_reservation_required = deal.optJSONObject("restrictions").optInt("is_reservation_required");
				_is_refundable = deal.optJSONObject("restrictions").optInt("is_refundable");
				_special_tips = deal.optJSONObject("restrictions").optString("special_tips");
				_notice = deal.optString("notice");
				_deal_url = deal.optString("deal_url");
				_deal_html5_url = deal.optString("deal_h5_url");
				_commission_ratio = deal.getDouble("commission_ratio");

				JSONArray businesses = deal.optJSONArray("businesses");
				if (businesses != null)
				{
					_businesses = new Business[businesses.length()];
					_businesses_count = businesses.length();

					for (int i=0; i<businesses.length(); i++)
					{
						_businesses[i] = new DianpingBusiness(dianping, businesses.getJSONObject(i));
					}
				}
			}
		} catch (JSONException e) {Function.errorDialog(e.toString());}
	}

	//团购单ID
	public String getDealID() {return _deal_id;}

	//团购标题
	public String getTitle() {return _title;}

	//团购描述
	public String getDescription() {return _description;}

	//城市名称，city为＂全国＂表示全国单，其他为本地单，城市范围见相关API返回结果
	public String getCity() {return _city;}

	//团购包含商品原价值
	public String getListPrice() {return _list_price;}

	//团购价格
	public String getCurrentPrice() {return _current_price;}

	//团购适用商户所在商区
	public String[] getRegions() {return _regions;}

	//团购所属分类
	public String[] getCategories() {return _categories;}

	//团购当前已购买数
	public int getPurchaseCount() {return _purchase_count;}

	//团购发布上线日期
	public String getPublishDate() {return _publish_date;}

	//团购详情
	public String getDetails() {return _details;}

	//团购单的截止购买日期
	public String getPurchaseDeadline() {return _purchase_deadline;}

	//团购图片链接，最大图片尺寸450×280
	public String getImageURL() {return _image_url;}

	//小尺寸团购图片链接，最大图片尺寸160×100
	public String getSmallImageURL() {return _small_image_url;}

	//更多大尺寸图片
	public String[] getMoreImageURLs() {return _more_image_urls;}

	//更多小尺寸图片
	public String[] getMoreSmallImageURLs() {return _more_small_image_urls;}

	//是否为热门团购，0：不是，1：是
	public int getIsPopular() {return _is_popular;}

	//团购限制条件：是否需要预约，0：不是，1：是
	public int getIsReservationRequired() {return _is_reservation_required;}

	//团购限制条件：是否支持随时退款，0：不是，1：是
	public int getIsRefundable() {return _is_refundable;}

	//团购限制条件：附加信息(一般为团购信息的特别提示)
	public String getSpecialTips() {return _special_tips;}

	//重要通知(一般为团购信息的临时变更)
	public String getNotice() {return _notice;}

	//团购Web页面链接，适用于网页应用
	public String getDealURL() {return _deal_url;}

	//团购HTML5页面链接，适用于移动应用和联网车载应用
	public String getDealHTML5URL() {return _deal_html5_url;}

	//当前团单的佣金比例
	public double getCommissionRatio() {return _commission_ratio;}

	//团购所适用的商户列表
	public Business[] getBusinesses() {return _businesses;}

	public int getBusinessesCount() {return _businesses_count;}
}